package svc

import (
	"fmt"
	"net/http"
	"strconv"

	"gitee.com/jn-shao/common"
	"github.com/gin-gonic/gin"

	"svcApi/api"
	"svcApi/forms"
	svc2 "svcApi/forms/svc"
	"svcApi/global"
	svc "svcApi/proto/svcSrv"
)

func FindSvcById(ctx *gin.Context) {
	fmt.Println("接受到 svcApi.FindSvcById 的请求")
	//获取svcId 参数
	id := ctx.Param("id")
	svcId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "参数异常",
		})
		return
	}
	//获取svc相关信息
	svcInfo, err := global.SvcService.FindSvcByID(ctx, &svc.SvcId{
		Id: svcId,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "内部错误",
		})
		return
	}
	//json 返回svc信息
	ctx.JSON(http.StatusOK, gin.H{
		"data": svcInfo,
	})
}

func AddSvc(ctx *gin.Context) {
	fmt.Println("接受到 svcApi.AddSvc 的请求")
	addSvcInfo := &svc2.SvcForm{}
	if err := ctx.ShouldBind(&addSvcInfo); err != nil {
		common.Error(err)
		api.HandleValidatorError(ctx, err)
		return
	}
	var svcPort []*svc.SvcPort
	for _, port := range addSvcInfo.SvcPort {
		svcPort = append(svcPort, &svc.SvcPort{
			SvcPort:         port.SvcPort,
			SvcTargetPort:   port.SvcTargetPort,
			SvcNodePort:     port.SvcNodePort,
			SvcPortProtocol: port.SvcPortProtocol,
		})
	}
	response, err := global.SvcService.AddSvc(ctx, &svc.SvcInfo{
		SvcNamespace:    addSvcInfo.SvcNamespace,
		SvcName:         addSvcInfo.SvcName,
		SvcTeamId:       addSvcInfo.SvcTeamId,
		SvcPodName:      addSvcInfo.SvcPodName,
		SvcExternalName: addSvcInfo.SvcExternalName,
		SvcPort:         svcPort,
		SvcType:         addSvcInfo.SvcType,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": response.Msg,
	})
}

func DeleteSvcById(ctx *gin.Context) {
	fmt.Println("接受到 svcApi.DeleteSvcById 的请求")
	id := ctx.Param("id")
	svcId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": "参数异常",
		})
		return
	}
	//删除指定服务
	response, err := global.SvcService.DeleteSvc(ctx, &svc.SvcId{
		Id: svcId,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": response.Msg,
	})
}

func UpdateSvc(ctx *gin.Context) {
	fmt.Println("接受到 svcApi.UpdateSvc 的请求")
	updateSvcInfo := &svc2.SvcForm{}
	if err := ctx.ShouldBind(&updateSvcInfo); err != nil {
		common.Error(err)
		api.HandleValidatorError(ctx, err)
		return
	}
	var svcPort []*svc.SvcPort
	for _, port := range updateSvcInfo.SvcPort {
		svcPort = append(svcPort, &svc.SvcPort{
			Id:              port.Id,
			SvcId:           port.SvcId,
			SvcPort:         port.SvcPort,
			SvcTargetPort:   port.SvcTargetPort,
			SvcNodePort:     port.SvcNodePort,
			SvcPortProtocol: port.SvcPortProtocol,
		})
	}

	id := ctx.Param("id")
	svcId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		common.Error(err)
		ctx.Status(http.StatusNotFound)
		return
	}

	response, err := global.SvcService.UpdateSvc(ctx, &svc.SvcInfo{
		Id:              svcId,
		SvcNamespace:    updateSvcInfo.SvcNamespace,
		SvcName:         updateSvcInfo.SvcName,
		SvcTeamId:       updateSvcInfo.SvcTeamId,
		SvcPodName:      updateSvcInfo.SvcPodName,
		SvcExternalName: updateSvcInfo.SvcExternalName,
		SvcPort:         svcPort,
		SvcType:         updateSvcInfo.SvcType,
	})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"msg": response.Msg,
	})
}

func FindSvcList(ctx *gin.Context) {
	fmt.Println("接受到 svcApi.FindSvcList 的请求")
	page := &forms.Page{}
	if err := ctx.ShouldBindUri(&page); err != nil {
		common.Error(err)
		api.HandleValidatorError(ctx, err)
		return
	}

	svcList, err := global.SvcService.FindSvcList(ctx, &svc.FindSvc{Pages: page.Pages, PagePerNums: page.PagePerNums})
	if err != nil {
		common.Error(err)
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"msg": err.Error(),
		})
		return
	}
	reMap := gin.H{
		"total": svcList.Total,
	}

	reMap["data"] = svcList.Data
	ctx.JSON(http.StatusOK, reMap)
}
